//
//  YSAVPlayerManager.m
//  Pods-YSPlayer_iOS_Example
//
//  Created by 宋宏康 on 2020/2/12.
//

#import "YSAVPlayerManager.h"
#import "YSAVPlayerHelper.h"

@interface YSAVPlayerManager()
/// 封面图
@property (nonatomic, strong) UIImageView *containerView;
/// 当前的view
@property (nonatomic, strong) UIView *currentView;
/// 播放器的frame
@property (nonatomic, assign) CGRect frame;
/// 播放的view
@property (nonatomic, strong) ZFPlayerControlView *controlView;
/// 播放的控制器
@property (nonatomic, strong) ZFPlayerController *player;
/// 播放按钮
@property (nonatomic, strong) UIButton *playBtn;
@end


@implementation YSAVPlayerManager


- (instancetype)initWithCustomFrame:(CGRect)frame withCurrentView:(UIView *)currentView
{
    self = [super init];
    if (![currentView isKindOfClass:UIView.class]) {
        
    }
    if (self) {
        self.currentView = currentView;
        self.frame = frame;
        [self initUI];
        [self initObj];
        [self initClick];
    }
    return self;
}

- (instancetype)initWithPlayerView:(UIView *)view
{
    if (![view isKindOfClass:UIView.class]) {
        
    }
    if (self) {
        self.currentView = view;
        self.frame = CGRectZero;
        [self initUI];
        [self initObj];
        [self initClick];
    }
    return self;
}

- (void)initUI
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
    _containerView = imageView;
    [_currentView addSubview:imageView];
    
    if (CGRectEqualToRect(self.frame, CGRectZero)) {
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    ///播放按钮
    UIButton *playerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _playBtn = playerBtn;
    [playerBtn setImage:[YSAVPlayerHelper imageWithName:@"new_allPlay_44x44_"] forState:UIControlStateNormal];
    [_containerView addSubview:playerBtn];
    [playerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
}

- (void)initClick
{
    @weakify(self);
    [[self.playBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (self.playerIndex > self.assetURLs.count) {
            self.playerIndex = 0;
        }
        [self.player playTheIndex:self.playerIndex];
        [self.controlView showTitle:[self.titles objectOrNilAtIndex:self.playerIndex] coverURLString:self.corverNetImgUrl fullScreenMode:ZFFullScreenModeLandscape];
    }];
    
    /// 播放完成
    self.player.playerDidToEnd = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset) {
        @strongify(self);
        if (self.playDidFinish) self.playDidFinish();
    };
     
   self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
       @strongify(self)
       [self.currentView.viewController setNeedsStatusBarAppearanceUpdate];
   };
    
    self.player.pauseWhenAppResignActive = YES;
    
    
    ///当前控制器将要出现
    [[self.currentView.viewController rac_signalForSelector:@selector(viewWillAppear:)]
     subscribeNext:^(RACTuple * _Nullable x) {
        @strongify(self);
        self.player.viewControllerDisappear = NO;
    }];
    
    ///当前控制器将要消失
    [[self.currentView.viewController rac_signalForSelector:@selector(viewWillDisappear:)]
    subscribeNext:^(RACTuple * _Nullable x) {
        @strongify(self);
        self.player.viewControllerDisappear = YES;
    }];
    
}

- (void)initObj
{
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    /// 播放器相关
     self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
     self.player.controlView = self.controlView;
     /// 设置退到后台继续播放
     self.player.pauseWhenAppResignActive = NO;
//    self.player.pauseByEvent = YES;
}

- (void)setAssetURLs:(NSArray<NSURL *> *)assetURLs
{
    _assetURLs = assetURLs;
    self.player.assetURLs = assetURLs;
}

- (void)setTitles:(NSArray<NSString *> *)titles
{
    _titles = titles;
}

- (void)setCorverNetImgUrl:(NSString *)corverNetImgUrl
{
    _corverNetImgUrl = corverNetImgUrl;
    [self.containerView setImageWithURLString:corverNetImgUrl placeholder:[ZFUtilities imageWithColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1] size:CGSizeMake(1, 1)]];
}

- (void)setPauseWhenAppResignActive:(BOOL)pauseWhenAppResignActive
{
    _pauseWhenAppResignActive = pauseWhenAppResignActive;
    self.player.pauseWhenAppResignActive = pauseWhenAppResignActive;
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [[ZFPlayerControlView alloc] init];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = YES;
    }
    return _controlView;
}

- (void)setViewControllerDisappear:(BOOL)viewControllerDisappear
{
    _viewControllerDisappear = viewControllerDisappear;
    self.player.viewControllerDisappear = viewControllerDisappear;
}

- (void)setStatusBarHidden:(BOOL)statusBarHidden
{
    _statusBarHidden = statusBarHidden;
    self.player.statusBarHidden = statusBarHidden;
}
#pragma mark - 功能方法
- (void)playerIndex:(NSInteger)index
{
    [self playerIndex:index];
}

- (void)playerReplay
{
    [self.player.currentPlayerManager  replay];
}

- (void)playerStop
{
    [self.player stop];
}

- (void)playTheNext
{
    if (!self.player.isLastAssetURL) {
         NSString *title = [self.titles objectOrNilAtIndex:(self.player.currentPlayIndex + 1)];
         [self.controlView showTitle:title coverURLString:self.corverNetImgUrl fullScreenMode:ZFFullScreenModeLandscape];
        [self.player playTheNext];
    }else{
        [self playerReplay];
    }
}

- (void)playThePrevious
{
    if (!self.player.isFirstAssetURL) {
        NSString *title = [self.titles objectOrNilAtIndex:(self.player.currentPlayIndex - 1)];
        [self.controlView showTitle:title coverURLString:self.corverNetImgUrl fullScreenMode:ZFFullScreenModeLandscape];
        [self.player playThePrevious];
    }else{
       [self playerReplay];
    }
}

- (void)dealloc
{
    NSLog(@"%s",__func__);
}
@end
